<template>
	<view class="uniapp">
		<view class="search-box">
			<my-search @click="gotoSearch"></my-search>
		</view>
		<!-- 轮播图 -->
		<view>
			<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
				<swiper-item v-for="(item,index) in swiperList" :key="index">
					<navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id">
						<image :src="item.image_src" mode="aspectFill"></image>
					</navigator>
					<!-- <navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?${item.goods_id}`">
						<image :src="item.image_src" mode="aspectFill"></image>
					</navigator> -->
				</swiper-item>
			</swiper>
		</view>
		<!-- 导航栏 -->
		<view class="nav-list">
			<view class="nav-item" v-for="(item, index) in navList" :key="index" @click="navClickHandle(item)">
				<image :src="item.image_src" mode="aspectFill" class="nav-image"></image>
			</view>
		</view>
		<!-- 楼层 -->
		<view class="floor-list">
			<view class="floor-item" v-for="(item, index) in floorList" :key="index">
				<!-- 标题 -->
				<image :src="item.floor_title.image_src" mode="aspectFill" class="floor-title"></image>
				<!-- 图片 -->
				<view class="floor-image-box">
					<!-- 左侧图片 -->
					<navigator class="left-image-box" :url="item.product_list[0].url">
						<image :src="item.product_list[0].image_src" mode="widthFix" :style="{width: item.product_list[0].image_width + 'rpx'}"></image>
					</navigator>
					<!-- 右侧图片 -->
					<view class="right-image-box">
						<navigator class="right-image-item" v-for="(item2,index2) in item.product_list" :key="index2" :url="item2.url">
							<image :src="item2.image_src" mode="widthFix" :style="{width: item2.image_width + 'rpx'}" v-if="index2 !== 0"></image>
						</navigator>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				swiperList:[], // 轮播图数据
				navList:[], // 导航栏数据
				floorList:[] // 楼层数据
			};
		},
		onLoad() {
			this.getSwiperList(),
			this.getNavList(),
			this.getFloorList()
		},
		methods:{
			// 获取轮播图数据
			async getSwiperList(){
				const {data:res} = await uni.$http.get('/api/public/v1/home/swiperdata')
				//console.log('轮播图',res)
				if(res.meta.status !== 200){
					return uni.$showMsg()
				}
				this.swiperList = res.message
				// uni.$showMsg('数据请求成功')
				/* this.swiperList.forEach(item => {
				  console.log(item.image_src);  // 确保是正确的图片 URL
				}); */
			},
			// 获取导航栏数据
			async getNavList(){
				const {data:res} = await uni.$http.get('/api/public/v1/home/catitems')
				// console.log('导航栏',res)
				if(res.meta.status !== 200){
					return uni.$showMsg()
				}
				this.navList = res.message
			},
			// 判断点击的导航栏，跳转页面
			navClickHandle(item){
				//console.log(item)
				if(item.name === '分类'){
					uni.switchTab({
						url:"/pages/cate/cate"
					})
				}
			},
			// 获取楼层数据
			async getFloorList() {
				try{
					const {data:res} = await uni.$http.get('/api/public/v1/home/floordata')
					//console.log('楼层',res)
					res.message.forEach(floor => {
						floor.product_list.forEach(prod => {
							prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
						})
					})
					this.floorList = res.message
				}catch(error){
					uni.$showMsg()
				}
			},
			gotoSearch(){
				uni.navigateTo({
					url:'/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
.search-box{
	position: sticky;
	top: 0;
	z-index: 999;
}
swiper{
	height: 300rpx;
	.swiper-item,
	image{
		width: 100%;
		height: 100%;
	}
}
.nav-list{
	display: flex;
	justify-content: space-around;
	margin: 15px 0;
	.nav-image{
		width: 128rpx;
		height: 140rpx;
	}
	
}
.floor-title{
	height: 60rpx;
	width: 100%;
	display: flex;
}
.right-image-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-image-box {
  display: flex;
  padding-left: 10rpx;
}

</style>
